package com.example.zhangguoqing.homeofpet;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

    private FrameLayout homeContent;
    private RadioGroup radioGroup;
    private RadioButton rbData,rbTip,rbInfo;
    static final  int NUM_ITEMS=4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题,
        setContentView(R.layout.activity_main);
        initView();
    }

    public void initView(){
        homeContent=(FrameLayout)findViewById(R.id.homeContent);
        radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
        rbData=(RadioButton)findViewById((R.id.rbData));
        rbTip=(RadioButton)findViewById(R.id.rbTip);
        rbInfo=(RadioButton)findViewById(R.id.rbInfo);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int index=0;
                switch(checkedId){
                    case R.id.rbData:
                        index=0;
                        break;
                    case R.id.rbTip:
                        index=1;
                        break;
                    case R.id.rbInfo:
                        index=2;
                        break;

                }
                Fragment fragment=(Fragment)adapter.instantiateItem(homeContent,index);
                adapter.setPrimaryItem(homeContent,0,fragment);
                adapter.finishUpdate(homeContent);
            }
        });
    }
    FragmentStatePagerAdapter adapter=new FragmentStatePagerAdapter(getSupportFragmentManager()) {

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }
        @Override
        public Fragment getItem(int i) {
            Fragment fragment=null;
            switch (i){
                case 0:
                    fragment=new DataFragment();
                    break;
                case 1:
                    fragment=new TipFragment();
                    break;
                case 2:
                    fragment=new InfoFragment();
                    break;

                default:
                    fragment=new DataFragment();
                    break;
            }
            return fragment;
        }
    };
    public void onStart(){
        super.onStart();
        radioGroup.check(R.id.rbData);
    }
}
